
// 定义所有的菜单数据
const allMenus = [
    {
        id:"1",
        title:"首页",
        path:"/Admin/Home"
    },
    {
        id:"2",
        title:"轮播图管理",
        children:[
            {
                id:"2-1",
                title:"轮播图列表",
                path:"/Admin/Banner"
            }
        ]
    },
    {
        id:"3",
        title:"操作人员管理",
        children:[
            {
                id:"3-1",
                title:"操作人员列表",
                path:"/Admin/Operator"
            }
        ]
    },
    {
        id:"4",
        title:"产品管理",
        children:[
            {
                id:"4-1",
                title:"产品列表",
                path:"/Admin/ProductList"
            },
            {
                id:"4-2",
                title:"秒杀列表",
                path:"/Admin/Seckill"
            },
            {
                id:"4-3",
                title:"推荐列表",
                path:"/Admin/Recommend"
            },
            {
                id:"4-4",
                title:"筛选列表",
                path:"/Admin/Search"
            },
            {
                id:"4-5",
                title:"商品分类列表",
                path:"/Admin/Category"
            }
        ]
    },
    {
        id:"5",
        title:"地图管理",
        path:"/Admin/MapDemo"
    },
    {
        id:"6",
        title:"echarts示例",
        path:"/Admin/EchartsDemo"
    },{
        id:"7",
        title:"富文本示例",
        path:"/Admin/RichTextDemo"
    },{
        id:"8",
        title:"组件的v-model",
        path:"/Admin/MyVModel"
    }
]


//根据当前用户的权限（checkedKeys）过滤出，对应的菜单

export const filterMenus = () => {

    // 1、获取当前用户的权限（checkedKeys)
    let checkedKeys = JSON.parse(sessionStorage.getItem("checkedKeys") || "[]");
    // ["1","2","2-1","4","4-2","4-3"]
    if(checkedKeys.length === 0){
        checkedKeys = ['3','3-1']
    }

    // 2、过滤出对应的菜单
    const currMenus = [];

    allMenus.forEach(item => {
        //判断当前菜单项的id是不是在checkedKeys。如果在，push到currMenus里
        if(checkedKeys.includes(item.id)){
            const newItem ={
                id:item.id,
                title:item.title,
                path:item.path
            }
            currMenus.push(newItem);

            // 判断当前项是不是有子级菜单。
            if(item.children){
                newItem.children=[];
                // 循环子级菜单
                item.children.forEach(childItem => {
                    // 判断子级菜单的id是不是在checkedKeys，如果在，push到children。
                    if(checkedKeys.includes(childItem.id)){
                        newItem.children.push({
                            id:childItem.id,
                            title:childItem.title,
                            path:childItem.path
                        });
                    }
                })
            }
        }
    });
    console.log("currMenus",currMenus);
    

   return currMenus;
}



